import time
import Config
from tools import MoveUtil, YoloV8Util, ActionUtil, FindDoorUtil, CommonUtil, FightingUtil, ImageUtil, WindowUtil


class FightCard:
    def __init__(self, map_name, position_grab, model):

        self.position_grab = position_grab

        self.model = model

        self.set_img = "images/set.bmp"

        self.back_town_img = "images/back_town_set.bmp"

        self.confirm_img = "images/confirm.bmp"

        # 随机移动次数
        self.move_random_index = 0

        # 技能列表
        self.skill_list = ["a", "s", "d", "f", "g"]

        # 当前技能索引
        self.skill_index = 2

        # 地图
        self.map_name = map_name

        # “再次挑战”尝试次数
        self.game_over_step = 0

    def move_random(self, seconds):
        # print(f"随机移动【{random_index}】次")
        self.move_random_index += 1
        # 每随机移动3次，时间加倍
        if self.move_random_index > 2:
            seconds = round(self.move_random_index / 3 * seconds, 1)
        if seconds > 2:
            # 重新初始化
            self.move_random_index = 0
        # 等待移动动作完成
        return MoveUtil.move_random(seconds)

    def back_to_town(self, position_grab, window, data_dic):
        # 没有敌人，则判断是否开门
        is_open, is_open_loc = FindDoorUtil.is_open(data_dic, Config.direction)
        print(f"是否开门：{is_open} {is_open_loc}")
        if not is_open:
            return False

        else:
            print("准备返回城镇！！！")
            # 打开游戏设置
            flag, loc = ImageUtil.is_icon_exist(window, self.set_img, (15, 15))
            print(f"[set_img]: {flag} {loc}")
            if flag:
                ActionUtil.mouse_left_click((loc[0], loc[1]))
                time.sleep(2)

            # 返回城镇
            window = WindowUtil.grab_screen(position_grab)
            flag, loc = ImageUtil.is_icon_exist(window, self.back_town_img, (20, 20))
            print(f"[back_town_img]: {flag} {loc}")
            if flag:
                ActionUtil.mouse_left_click((loc[0], loc[1]))
                time.sleep(2)

            # 查找“确定按钮”
            window = WindowUtil.grab_screen(position_grab)
            flag, loc = ImageUtil.is_icon_exist(window, self.confirm_img, (26, 16))
            print(f"[confirm_img]: {flag} {loc}")
            if flag:
                ActionUtil.mouse_left_click((loc[0], loc[1]))
                self.skill_index = 2
                time.sleep(8)
                return True

            return False

    def fight_main(self):
        while True:
            window_rgb = WindowUtil.grab_screen(self.position_grab)
            data_dic = YoloV8Util.predict(self.model, window_rgb)
            # 刷新角色的坐标
            role_flag, location = YoloV8Util.get_data_one(data_dic, Config.me, False)
            if role_flag is False:
                print("未找到角色，重新获取")
                self.move_random(0.5)
                continue

            # 当前没有移动和战斗，则寻找敌人
            enemys = YoloV8Util.get_data_all(data_dic, Config.enemy, False)
            pick_ups = YoloV8Util.get_data_all(data_dic, Config.pickup)
            print(f"[enemys]:{len(enemys)}   [pick_ups]:{len(pick_ups)}")

            if len(enemys) == 0 and len(pick_ups) == 0:
                print("暂停1秒，等待回城")
                time.sleep(1)

                if self.back_to_town(self.position_grab, window_rgb, data_dic):
                    return True, 0
                else:
                    # 啥都没有就动一下
                    print("返回城镇标记，查找失败，随机动一下")
                    self.move_random(1)
                    continue

            else:
                self.move_random_index = 0
                # 获取最近的坐标
                fight_flag, near_loc = CommonUtil.get_near_type_loc(location, enemys, pick_ups)
                if not fight_flag:
                    # 捡东西
                    MoveUtil.moveTo(location, near_loc)
                else:
                    # 打怪
                    # flag = MoveUtil.is_need_enter(location, near_loc, distance=150)
                    # if not flag:
                        MoveUtil.moveTo(location, near_loc)
                    # else:
                        # 开始战斗！！！
                        if len(self.skill_list) > 0:
                            # 不管有没有移动距离，都开始一套技能
                            # 避免角色和敌人重叠，无需移动的情况
                            fight_finish, self.skill_index = FightingUtil.fighting(self.skill_list, self.skill_index)
